package com.wx.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wx.domain.Drug;
import com.wx.service.DrugService;
import com.wx.service.impl.DrugServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/addDrug")
public class AddDrug extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=UTF-8");
        ObjectMapper mapper=new ObjectMapper();
        Map<String, Object> map=new HashMap<>();

        String drugName = req.getParameter("drugName");
        String drugExplain = req.getParameter("drugExplain");
        int drugNumber = Integer.parseInt(req.getParameter("drugNumber"));
        Double drugPrice = Double.valueOf(req.getParameter("drugPrice"));
        String drugType = req.getParameter("drugType");
        String doctorId = req.getParameter("doctorId");

        Drug drug=new Drug();
        drug.setDrugName(drugName);
        drug.setDrugExplain(drugExplain);
        drug.setDrugNumber(drugNumber);
        drug.setDrugPrice(drugPrice);
        drug.setDrugType(drugType);

        DrugService drugService=new DrugServiceImpl();
        boolean isTrue = drugService.addDrug(drug,doctorId);

        map.put("isTrue",isTrue);
        mapper.writeValue(resp.getWriter(),map);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
